'''Game client'''

import game_thread

class ClientHandler(game_thread.Handler):
    def __init__(self, my_q, client):
        super(ClientHandler, self).__init__(my_q)

        self.client_name = client
        self.game_state = None
        self.cards = None
        self.game_id = None


    def connect_to_server(self, server_q):
        self.server_q = server_q

        game_thread.GameThread.ntfy(self.server_q, 'register_client', self.client_name, self.q)

    def new_game(self, game_id):
        game_thread.GameThread.ntfy(self.server_q, 'new_game', game_id)

    def list_games(self):
        return game_thread.GameThread.rpc(self.server_q, 'list_games')

    def join_game(self, game_id, as_character):
        game_thread.GameThread.ntfy(self.server_q, 'join_game', self.client_name, game_id, as_character)
        self.game_id = game_id

    def start_game(self):
        game_thread.GameThread.ntfy(self.server_q, 'start_game', self.game_id)

    def move_player(self, character, new_pos):

#        print 'moving %s to %s' % (character, repr(new_pos))
        
        game_thread.GameThread.ntfy(self.server_q, 'move_player', self.game_id, self.client_name, character, new_pos)

    def make_suggestion(self, sug_character, sug_weapon, sug_room):
        game_thread.GameThread.ntfy(self.server_q, 'make_suggestion', self.game_id, self.client_name, sug_character, sug_weapon, sug_room)

    def suggestion_response(self, dest_player_id, response):
        game_thread.GameThread.ntfy(self.server_q, 'suggestion_response', self.game_id, self.client_name, dest_player_id, response)

    def turn_completed(self):
        game_thread.GameThread.ntfy(self.server_q, 'turn_completed', self.game_id, self.client_name)

    def make_accusation(self, character, weapon, room):
        game_thread.GameThread.ntfy(self.server_q, 'make_accusation', self.game_id, self.client_name, character, weapon, room)

    def query_house(self):
        game_thread.GameThread.ntfy(self.server_q, 'query_house', self.game_id, self.client_name)

    def query_cards(self):
        print \
'''

#### Cards dealt to client %s ##############
    %s
#############################################
        
''' % (self.client_name, '\n    '.join([repr(c) for c in self.cards]))

    # server notifications
    def query_house_response(self, game_id, loc):
        import location_debug
        location_debug.print_locations(loc)
    
    def state_update(self, game_state):
        self.game_state = game_state
        print \
'''

#### Game state update for client %s #######
game id:   %s
can_start: %s
started:   %s
round:     %s
turn:      %s
game_over:   %s
game_winner: %s
characters:
    %s
weapons:
    %s
players:
    %s
#############################################

''' % (self.client_name,   \
       game_state.game_id, \
       repr(game_state.can_start), \
       repr(game_state.started),   \
       repr(game_state.round),     \
       game_state.player_turn,     \
       repr(game_state.game_over), \
       game_state.game_winner,     \
       '\n    '.join(['%s in location %s' % (repr(c), repr(l)) for (c,l) in game_state.characters]), \
       '\n    '.join(['%s in location %s' % (repr(c), repr(w)) for (c,w) in game_state.weapons]),
       '\n    '.join(['%s, playing as %s%s' % (p.player_id, p.character_choice, ' has made a false accusation' if p.false_accusation else '') for p in game_state.players]))
        


    def take_turn(self, game_id):
        if game_id != self.game_id:
            raise Exception('%s: Receiving notification for wrong game: %s, expecting: %s' % (self.client_name, game_id, self.game_id))
        
        print \
'''

#### Take turn notification for client %s ####
#############################################

''' % (self.client_name)

    def deal_cards(self, game_id, cards):
        if game_id != self.game_id:
            raise Exception('%s: Receiving notification for wrong game: %s, expecting: %s' % (self.client_name, game_id, self.game_id))        

        self.cards = cards

        self.query_cards()


    def respond_to_suggestion(self, game_id, player_id, sug_character, sug_weapon, sug_room):
        if game_id != self.game_id:
            raise Exception('%s: Receiving notification for wrong game: %s, expecting: %s' % (self.client_name, game_id, self.game_id))        

        print \
'''

#### %s has made a suggestion, %s please respond ###
Suggestion:
    Done by:  %s
    With the: %s
    In the:   %s
####################################################
    
''' % (player_id, self.client_name, sug_character, sug_weapon, sug_room)
                    
    def no_suggestion_responses(self, game_id):
        if game_id != self.game_id:
            raise Exception('%s: Receiving notification for wrong game: %s, expecting: %s' % (self.client_name, game_id, self.game_id))

        print \
"""

#### Nobody was able to respond to %s's suggestion ###
######################################################
    
""" % (self.client_name)
        

    def received_suggestion_response(self, game_id, player_id, response):
        print \
"""

#### %s responded to %s's suggestion ###
Response:
    %s
####################################################
    
""" % (player_id, self.client_name, response)

    def game_over(self, game_id, player_id, character, weapon, room):
        print \
"""
#### GAME OVER ##############################
#### Player %s made a correct accusation ####
Suggestion:
    Done by:  %s
    With the: %s
    In the:   %s
#############################################
    
""" % (player_id, character, weapon, room)
        
        
    def false_accusation(self, game_id, player_id, character, weapon, room):
        print \
"""
#### Player %s made a false accusation ####
Suggestion:
    Done by:  %s
    With the: %s
    In the:   %s
#############################################
    
""" % (player_id, character, weapon, room)
        
    def game_list_update(self, game_list):
        print \
'''

#### New Game List for client %s####
    %s
####################################

''' % (self.client_name, '\n    '.join(game_list))

        


class CluelessClient(game_thread.GameThread):
    def __init__(self, client_name):
        super(CluelessClient, self).__init__('Client: %s' % client_name, ClientHandler, client_name)


if __name__ == '__main__':
   pass
